package io.gitee.caoxiaoyu97.configcenter.client;

import com.cxy.common.boot.CxyCloudConstants;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;

import java.util.Map;

/*********************************************************
 * 文件名称：ConfigClient.java
 * 系统名称：CXY自定义系统
 * 模块名称：io.gitee.caoxiaoyu97.configcenter
 * 功能说明：
 * 开发人员 @author：caoxy31978
 * 开发时间 @date：2023/7/5 10:33
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
@Order
public class ConfigClient implements EnvironmentPostProcessor {

    public static final String CONFIGCENTER_CLIENT_ENABLED = "configcenter.client.enabled";
    public static final String CONFIGCENTER_RUL = "configcenter.rul";

    private final Log LOGGER;

    public ConfigClient(DeferredLogFactory logFactory) {
        this.LOGGER = logFactory.getLog(ConfigClient.class);
    }
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        if (verifyLaunchConfig(environment)){
            initConfigParam(environment);
            Map<String, Object> remoteConfig = RemoteConfigService.getRemoteConfig();
            if (ObjectUtils.isNotEmpty(remoteConfig)){
                environment.getPropertySources().addFirst(new MapPropertySource("cxyRemoteConfig",remoteConfig));
            }else{
                LOGGER.warn("配置中心参数为空，请确认是否正常");
            }

        }
    }

    /**
     * 初始化配置中心需要的参数
     * @param environment
     */
    private void initConfigParam(ConfigurableEnvironment environment) {
        ConfigCenterParam instance = ConfigCenterParam.getInstance();
        instance.setUrl(environment.getProperty(CONFIGCENTER_RUL));
        instance.setAppNmae(environment.getProperty(CxyCloudConstants.APP_NAME));
    }

    /**
     * 校验是否开启本配置中心
     * @param environment
     * @return
     */
    private boolean verifyLaunchConfig(ConfigurableEnvironment environment) {
        Boolean enabled =
                environment.getProperty(CONFIGCENTER_CLIENT_ENABLED, Boolean.class, true);
        if (!enabled){
            LOGGER.info("cxy配置中心客户端未启用(配置项:"+ CONFIGCENTER_CLIENT_ENABLED+",default:true)");
            return false;
        }
        String appName = environment.getProperty(CxyCloudConstants.APP_NAME);
        if (StringUtils.isBlank(appName)){
            LOGGER.info("cxy配置中心客户端未启用(缺少"+CxyCloudConstants.APP_NAME+"配置)");
            return false;
        }
        String configCenterRul = environment.getProperty(CONFIGCENTER_RUL);
        if (StringUtils.isBlank(configCenterRul)){
            LOGGER.info("cxy配置中心客户端未启用(缺少"+CONFIGCENTER_RUL+"配置)");
            return false;
        }
        ConfigCenterParam.getInstance().setEnabled(true);
        return true;
    }
}
